package com.reguang.utils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;

/**
 * 
 * <p>Title: ObjectUtils</p>
 * <p>Description: object工具类</p>
 * @author weibiao
 * @date 2019年6月25日
 */
public class ObjectUtils {

	public static boolean isNull(Object obj) {
		return obj == null;
	}

	public static boolean isNotNull(Object obj) {
		return !isNull(obj);
	}

	public static boolean isEmpty(Object obj) {
		if (obj == null)
			return true;
		else if (obj instanceof CharSequence)
			return ((CharSequence) obj).length() == 0;
		else if (obj instanceof Collection)
			return ((Collection<?>) obj).isEmpty();
		else if (obj instanceof Map)
			return ((Map<?, ?>) obj).isEmpty();
		else if (obj.getClass().isArray())
			return Array.getLength(obj) == 0;
		else if (obj instanceof String)
			return "".equals(obj.toString().trim());
		return false;
	}

	public static boolean isNotEmpty(Object obj) {
		return !isEmpty(obj);
	}

	/**
	 * 
	 * <p>Title: propertiesCopy</p>
	 * <p>Description: 对象属性拷贝</p>
	 * @param source
	 * @param clazz
	 * @return
	 * @throws Exception
	 */
	public static <T> T propertiesCopy(Object source, Class<T> clazz) throws Exception {
		if (null == source) {
			return null;
		} else {
			T obj = clazz.newInstance();
			BeanUtils.copyProperties(source, obj);
			return obj;
		}
	}

	/**
	 * 
	 * <p>Title: collectionCopy</p>
	 * <p>Description: list中对象的copy</p>
	 * @param source
	 * @param clazz
	 * @return
	 * @throws Exception
	 */
	public static <T> List<T> collectionCopy(Collection<?> source, Class<T> clazz) throws Exception {
		if (isEmpty(source)) {
			return new ArrayList<T>();
		} else {
			List<T> list = new ArrayList<T>();
			Iterator<?> var3 = source.iterator();

			while (var3.hasNext()) {
				Object o = var3.next();
				list.add(propertiesCopy(o, clazz));
			}
			return list;
		}
	}
}
